#include "game/mycar.h"
#include <QPainter>
#include <QDebug>
#include <QKeyEvent>
#include <QOrientationReading>
#include <QGraphicsItem>
#include <QMovie>
#include <QLabel>

MyItem::MyItem()
{
    //设置小车可以获得焦点
    setFlag(QGraphicsItem::ItemIsFocusable);

    //设置小车支持悬停事件
    setAcceptHoverEvents(true);

    pix.load(":/resources/car.png");
    minSpeed = 0;
    speed = 0;
    this->setData( 1, 1);
    this->setPos( 260, 500);
    this->setFocus();
    //条件编译Android平台代码
#ifdef ANDROID
    //重力感应
    sensor = new QOrientationSensor(this);
    sensor->start();
    //当重力感应数据改变时发出readingChangeed()信号
    connect( sensor, SIGNAL(readingChanged()), this, SLOT(SensorEvent()));
#endif
}

/**
 * @brief 获取重力感应数据并转化为速度或方向控制
 *
 */
#ifdef ANDROID
void MyItem::SensorEvent()
{
    switch (sensor->reading()->orientation())
    {
    //方向为向上
    case QOrientationReading::TopUp :
        this->moveBy( 0, 10);
        speed += -1;
        emit sendSpeed(speed);
        break;
        //方向向下
    case  QOrientationReading::TopDown :
        this->moveBy( 0, -10);
        speed += 1;
        emit sendSpeed(speed);
        break;
        //方向向左
    case QOrientationReading::LeftUp :
        if ( this->x() == 350)
            break;
        this->moveBy( 10, 0);
        break;
        //方向想右
    case QOrientationReading::RightUp :
        if ( this->x() == 80)
            break;
        this->moveBy( -10, 0);
        break;
    case QOrientationReading::Undefined:
        break;
    case QOrientationReading::FaceDown:
        break;
    case QOrientationReading::FaceUp:
        break;
    }
}
#endif

/**
 * @brief 初始化小车参数
 *
 */
void MyItem::initParam()
{
    UsersDB *userdb = new UsersDB;
    UserModel user = userdb->getUserInfo();

    minSpeed = user.difficulty;
    speed = minSpeed;

    //设置是否可以触屏拖动
    if (user.touch)
        setFlag(QGraphicsItem::ItemIsMovable, true);
    else
        setFlag(QGraphicsItem::ItemIsMovable, false);
    emit sendSpeed(speed);
}

/**
 * @brief 初始化小车速度
 *
 */
void MyItem::initSpeed()
{
    speed = minSpeed;
}

/**
 * @brief 获取小车最小速度
 *
 * @return qreal
 */
qreal MyItem::getMinSpeed()
{
    return minSpeed;
}

/**
 * @brief 获取方向键事件，并做速度或方向控制（win7平台有用）
 *
 * @param event 键盘按键事件
 */
#ifdef WIN32
void MyItem::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case Qt::Key_Down :
        if ( speed == minSpeed)
        {
            emit sendSpeed(speed);
            break;
        }
        this->moveBy( 0, 10);
        speed += -1;
        emit sendSpeed(speed);
        break;
    case Qt::Key_Up :
        if (speed == 5)
        {
            emit sendSpeed(speed);
            break;
        }
        this->moveBy( 0, -10);
        speed += 1;
        emit sendSpeed(speed);
        break;
    case Qt::Key_Right :
        if ( this->x() == 350)
            break;
        this->moveBy( 10, 0);
        break;
    case Qt::Key_Left :
        if ( this->x() == 80)
            break;
        this->moveBy( -10, 0);
        break;
    }
    this->setFocus();
}
#endif

/**
 * @brief 设置控制的小车速度
 *
 * @param Speed 速度
 */
void MyItem::setSpeed(const qreal &Speed)
{
    this->speed = Speed;
}

/**
 * @brief 通过速度控制小车在背景上的相对移动
 *
 * @param phase
 */
void MyItem::work()
{
    this->moveBy( 0, -speed);

#ifdef ANDROID
    this->SensorEvent();
#endif
}

/**
 * @brief 绑定小车处理范围
 *
 * @return QRectF
 */
QRectF MyItem::boundingRect() const
{
    qreal adjust = 0.5;
    return QRectF(0 - adjust, 0 - adjust,
                  60 + adjust, 122 + adjust);
}

/**
 * @brief 重绘和重力检测
 *
 * @param painter
 * @param
 * @param
 */
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *,
                   QWidget *)
{
    //设置边框为透明
    painter->setPen(QPen(QColor( 0,0,0,0)));

    // 为小车使用贴图
    painter->drawPixmap( 0, 0, 60, 122, pix);
    QGraphicsItem *item;

    //小车碰撞处理
    if(!collidingItems().isEmpty())
    {
        foreach ( item, collidingItems()){
            if( item->data(1) == 1)
            {
                emit ending();
            }
        }
    }
    painter->drawRect(0, 0, 60, 122);
}
